<?php
$public = true;
if (!isset($common)) require_once 'include/common.php';
?>

<?php include 'ui/header.php'; ?>

<title>the tims</title>

<?php include 'ui/body.php'; ?>


<html>
  <head>
    <title>Sign in</title>
      </head>
  <body>
<?php
function form()
{
    ?>
        	<br />
        	<br />
        	<br />
    
    <form action="<?php $PHP_SELF;?>" method="post">
    
    <font color="white" size=4 face="arial">Usuario:</font> <input type="text" name="username" value=""/>
    <font color="white" size=4 face="arial">Contraseña:</font> <input type="password" name="password" value="" />
            	<br />
    
     <br />
    <input type="submit" value="Ok" />
 	<INPUT TYPE="BUTTON" VALUE="Register" ONCLICK="window.location.href='signup.php'"> 
    
    </form>
   
    
    <?
}

        if(isset($_COOKIE["username"]))
        // Section for logged users
        {
            echo "I know, you are ".$_COOKIE["username"].".";
            echo '<a href="signout.php">sign out</a>';
        }
        // Section for unlogged users
        else
        {
            if(isset($_REQUEST['username']) && isset($_REQUEST['password']))
            // Section for logging process
            {
                $username = trim($_REQUEST['username']);
                $password = trim($_REQUEST['password']);
                
                // Get the user from the DB
                require 'include/dbconnect.php';
                $result = mysql_query("SELECT username, password FROM users WHERE username='".$username."'");
                if( !$result)
                {
                	die('Error: '. mysql_error());
                }
                $row = mysql_fetch_array($result);
                if ( $row['password']==$password )
                {
                	/*
                	echo '<p><b><font color="white" size=5 face="arial">Bienvenido a The TIMS</font></b></p>';
               		echo '<a href="signout.php">sign out</a>';
               		*/
                	
                	header("Location:map.php");
               		
               		// Create the session and cookie
	                require 'include/class/usersession.php';
	                usersession::loadSession($username);
	                
	                echo '<a href="index.php>home</a>';
                }
                else
                { ?>
                	<font color="white" size=5 face="arial"><?php echo "Contraseña incorrecta";?> </font> <?php
                }
                
                require 'include/dbdisconnect.php';
            }
            else
			// Section for signin up
            {
                form();
            }
        }
?>
  </body>
</html>

<?php include 'ui/end.php'; ?>

